package com.wangsd.web.shop.service;

import com.gitee.fastmybatis.core.query.Query;
import com.gitee.fastmybatis.core.query.Sort;
import com.gitee.fastmybatis.core.util.MyBeanUtil;
import com.wangsd.web.shop.entity.Coupon;
import com.wangsd.web.shop.entity.CouponOrder;
import com.wangsd.web.shop.entity.Member;
import com.wangsd.web.shop.mapper.MemberMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class MemberService {
    @Resource
    MemberMapper mapper;
    @Autowired
    MemberIntegralService memberIntegralService;
    @Autowired
    MemberQiandaoService memberQiandaoService;

    public void updateMember(Member member) {
        mapper.updateIgnoreNull(member);
    }

    public void updatePayOpenid(String miniOpenid, String openid) {
        Member member = new Member();
        member.setPayOpenid(openid);
        mapper.updateByQuery(member, new Query().eq("mini_openid", miniOpenid));
    }

    @Transactional
    public int register(Member member) {
        Member old = this.selectByMiniOpenId(member.getMiniOpenid());
        if (old == null) {
            //判断是否分享注册
            if (StringUtils.isNotBlank(member.getShareUser())) {
                Member share = this.selectByMiniOpenId(member.getShareUser());
                //增加10助力点
                share.setAmount(10);
                this.updateMember(share);
            }
            mapper.saveIgnoreNull(member);
        }
        else {
            member.setId(old.getId());
            mapper.updateIgnoreNull(member);
        }
        return 1;
    }

    public Member selectByMiniOpenId(String miniOpenid) {
        return mapper.getByColumn("mini_openid", miniOpenid);
    }

    public List<Member> selectPage(Member queryObj, String loginCode, String sortField, String sortOrder) {
        Query query = new Query();
        if (StringUtils.isNotBlank(loginCode)) {
            query.likeRight("t.parent_code", loginCode);
        }
        if (StringUtils.isNotBlank(queryObj.getParentCode())) {
            query.likeRight("t.parent_code", queryObj.getParentCode());
        }
        if (StringUtils.isNotBlank(queryObj.getMobile())) {
            query.like("t.mobile", queryObj.getMobile());
        }
        if (StringUtils.isNotBlank(queryObj.getNickName())) {
            query.like("t.nick_name", queryObj.getNickName());
        }
        if (StringUtils.isNotBlank(sortField) && StringUtils.isNotBlank(sortOrder)) {
            String column = sortField;
            if ("createdTime".equals(sortField)){
                column = "created_time";
            }
            query.orderby("t." + column, "ascend".equals(sortOrder) ? Sort.ASC : Sort.DESC);
        }

        query.join("join customer t2 on t.parent_code = t2.code");
        List<String> column = Arrays.asList("t.*, t2.name as customerName");
        List<Map<String, Object>> mapList =  mapper.listMap(column, query);
        List<Member> list = MyBeanUtil.mapListToObjList(mapList, Member.class);
        return list;
    }

    @Transactional
    public void qiandao(String miniOpenid) {
        Member member = selectByMiniOpenId(miniOpenid);
        member.setIntegral(member.getIntegral() + 10);
        mapper.updateIgnoreNull(member);

        memberIntegralService.save(10, miniOpenid, null, 1, "签到赠送积分");

        memberQiandaoService.save(miniOpenid);
    }

    /**
     * 补录积分
     * @param id
     * @param integral
     * @param description
     */
    @Transactional
    public void additionalIntegral(Long id, Integer integral, String description) {
        Member member = mapper.getById(id);
        member.setIntegral(member.getIntegral() + integral);
        mapper.updateIgnoreNull(member);

        memberIntegralService.save(integral, member.getMiniOpenid(), null, 1, description);
    }

}
